package com.enterprise.projectManager.service.implemention;

import com.enterprise.projectManager.dto.JwtAuthenticationResponse;
import com.enterprise.projectManager.dto.LoginRequest;
import com.enterprise.projectManager.dto.RefreshTokenRequest;
import com.enterprise.projectManager.dto.SignUpRequest;
import com.enterprise.projectManager.model.Count;
import com.enterprise.projectManager.model.User;
import com.enterprise.projectManager.rope.CountRope;
import com.enterprise.projectManager.rope.UserRope;
import com.enterprise.projectManager.service.AuthenticationService;
import lombok.RequiredArgsConstructor;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Service;

import java.util.HashMap;

@Service
@RequiredArgsConstructor
public class AuthenticationServiceImpl implements AuthenticationService {
    private final UserRope userRope;
    private final PasswordEncoder passwordEncoder;
    private final CountRope countRope;
    private final AuthenticationManager authenticationManager;
    private final JWTServiceImpl jwtService;
    public User signUp(SignUpRequest signUpRequest)
    {
        User user = new User();
        Count count = countRope.findById(1L).get();
        user.setAccount(signUpRequest.getAccount());
        user.setPassword(passwordEncoder.encode(signUpRequest.getPassword()));
        user.setUsername(signUpRequest.getUserName());
        user.setRole("employee");
        user.setUserId("YG"+String.valueOf(count.getUserCount()));
        count.setUserCount(count.getUserCount()+1);

        countRope.save(count);

        return userRope.save(user);
    }

    public JwtAuthenticationResponse login(LoginRequest loginRequest){
        authenticationManager.authenticate(new UsernamePasswordAuthenticationToken(
                loginRequest.getAccount(),
                loginRequest.getPassword()));

        var user = userRope.findByAccount(loginRequest.getAccount()).orElseThrow(()-> new IllegalArgumentException("用户名或密码错误"));
        var jwt = jwtService.generateToken(user);
        var refreshToken = jwtService.generateRefreshToken(new HashMap<>(),user);

        JwtAuthenticationResponse jwtAuthenticationResponse = new JwtAuthenticationResponse();
        jwtAuthenticationResponse.setToken(jwt);
        jwtAuthenticationResponse.setRefreshToken(refreshToken);

        return jwtAuthenticationResponse;
    }

    public JwtAuthenticationResponse refreshToken(RefreshTokenRequest refreshTokenRequest)
    {
        String userAccount = jwtService.extractUserName(refreshTokenRequest.getToken());
        User user = userRope.findByAccount(userAccount).orElseThrow();
        if (jwtService.isTokenValid(refreshTokenRequest.getToken(),user))
        {
            var jwt = jwtService.generateToken(user);

            JwtAuthenticationResponse jwtAuthenticationResponse = new JwtAuthenticationResponse();
            jwtAuthenticationResponse.setToken(jwt);
            jwtAuthenticationResponse.setRefreshToken(refreshTokenRequest.getToken());

            return jwtAuthenticationResponse;
        }
        return null;
    }
}
